<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.nameCn"
          clearable
          placeholder="中文菜单名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.nameEn"
          clearable
          placeholder="英文菜单名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.sort"
          clearable
          placeholder="排序"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="450px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="140px">
          <el-form-item label="中文菜单名称">
            <el-input v-model="form.nameCn" style="width: 200px;" />
          </el-form-item>
          <el-form-item label="英文菜单名称">
            <el-input v-model="form.nameEn" style="width: 200px;" />
          </el-form-item>
          <el-form-item label="菜单图片">
            <el-upload
              ref="my-upload"
              :on-success="handleSuccess"
              :on-error="handleError"
              :headers="headers"
              :action="imagesUploadApi"
              :data="type"
              list-type="picture"
              :limit="1"
              class="avatar-uploader"
              :show-file-list="false"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="菜单路径">
            <el-input v-model="form.url" style="width: 200px;" />
          </el-form-item>
          <el-form-item label="排序">
            <el-input-number
              v-model.number="form.sort"
              :min="0"
              :max="999999"
              controls-position="right"
              style="width: 178px;"
            />
          </el-form-item>
          <el-form-item label="显示在我的页面">
            <el-switch v-model="form.showMyStatus" />
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch v-model="form.showStatus" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="normary" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        row-key="id"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="title" label="菜单名称"      >
          <template slot-scope="scope">
            <div>{{ scope.row.nameCn }}</div>
            <div>{{ scope.row.nameEn }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="pic" label="菜单图片"      >
          <template slot-scope="scope">
            <img :src="baseApi + scope.row.pic" style="height: 15px" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="url" label="菜单路径"       />
        <el-table-column prop="sort" label="排序" sortable="column"       />
        <el-table-column prop="showMyStatus" label="显示在我的页面"      >
          <template slot-scope="scope">
            <mall-switch
              :row="scope.row"
              :dict-data="dict.label.sysStatus"
              :row-data="scope.row.showMyStatus"
              row-filed="showMyStatus"
              :title="scope.row.nameCn"
            />
          </template>
        </el-table-column>
        <el-table-column prop="showStatus" label="是否显示"      >
          <template slot-scope="scope">
            <mall-switch
              :row="scope.row"
              :dict-data="dict.label.sysStatus"
              :row-data="scope.row.showStatus"
              row-filed="showStatus"
              :title="scope.row.nameCn"
            />
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','menu:edit','menu:del'])" label="操作" width="150px"      >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMenu from '@/api/ums/menu'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
import mallSwitch from '@/components/mallSwitch'

const defaultForm = {
  id: null,
  nameCn: null,
  nameEn: null,
  pic: null,
  url: null,
  sort: 0,
  showStatus: false,
  showMyStatus: false
}
export default {
  name: 'Menu',
  dicts: ['true_false', 'sysStatus'],
  components: { pagination, crudOperation, rrOperation, udOperation, mallSwitch },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      imageUrl: '',
      type: { type: 4 },
      headers: {
        'Authorization': getToken()
      },
      permission: {
        add: ['admin', 'menu:add'],
        edit: ['admin', 'menu:edit'],
        del: ['admin', 'menu:del']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'nameCn', display_name: '中文菜单名称' },
        { key: 'nameEn', display_name: '英文菜单名称' },
        { key: 'sort', display_name: '排序' }
      ]
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  cruds() {
    return CRUD({ title: 'app菜单', url: 'menu/page', sort: 'sort:desc', crudMethod: { ...crudMenu }})
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.imageUrl = this.baseApi + form.pic
    },
    [CRUD.HOOK.afterEditCancel]() {
      this.imageUrl = ''
    },
    [CRUD.HOOK.afterAddCancel]() {
      this.imageUrl = ''
    },
    [CRUD.HOOK.afterSubmit]() {
      this.imageUrl = ''
      this.clearFiles()
    },
    sortChange(column) {
      if (column.order === 'ascending') {
        this.crud.sort = [column.prop + ':asc']
      }
      if (column.order === 'descending') {
        this.crud.sort = [column.prop + ':desc']
      }
      this.crud.refresh()
    },
    handleExceed() {
      this.$message.warning('当前限制选择 1张图片，请重新选择')
    },
    handleSuccess(res, file) {
      if (res.code === 0) {
        this.form.pic = res.data.url
      } else {
        this.$message.error('图片上传失败,请重新上传')
      }
      this.imageUrl = URL.createObjectURL(file.raw)
      this.clearFiles()
    },
    handleError() {
      this.$message.error('图片上传失败,请重新上传')
    },
    clearFiles() {
      this.$refs['my-upload'].clearFiles()
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
